cmake_minimum_required(VERSION 3.28)

project(StudentManager C)
set(CMAKE_C_STANDARD 11)
set(BUILD_SHARED_LIBS OFF)
set(CMAKE_VERBOSE_MAKEFILE ON)

add_definitions(-DSTATIC_LIB)
add_definitions(-DSTATIC_LIBRARY)

if (MSVC)
    link_directories(lib-MSVC)
    add_compile_options(/utf-8)
    add_compile_options(/NODEFAULTLIB:library)
else ()
    link_directories(lib)

endif ()

include_directories(include include/glib-2.0)

add_executable(StudentManager
        src/main.c
        src/utils.c
        src/static_response.c
        src/frontend.c
        src/backend.c
)

if (MSVC)
    add_definitions(-D_WIN32_WINNT=0x0601)
    target_link_libraries(StudentManager
            PRIVATE
            Iphlpapi
            Userenv
            Dbghelp
            iconv
            intl
            libuv
            pcre2-8
            glib-2.0
            json-c
            sqlite3
    )
    add_custom_command(TARGET StudentManager POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_if_different
            ${CMAKE_SOURCE_DIR}/database.db
            $<TARGET_FILE_DIR:StudentManager>
    )
else ()
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}  -Wl,-Bstatic,--whole-archive -lwinpthread -Wl,--no-whole-archive")

    target_link_libraries(StudentManager
            PRIVATE
            libuv.a
            libpcre2-8.a
            glib-2.0
            libjson-c.a
            libsqlite3.a
            libintl.a
            DbgHelp
            IPHlpApi
            UserEnv
    )
    add_custom_command(TARGET StudentManager POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_if_different
            ${CMAKE_SOURCE_DIR}/database.db
            $<TARGET_FILE_DIR:StudentManager>
    )
endif ()

if (WIN32)
    target_link_libraries(StudentManager PRIVATE ws2_32)
endif ()

add_custom_target(copy_static ALL
        COMMAND ${CMAKE_COMMAND} -E copy_directory
        ${CMAKE_SOURCE_DIR}/static
        $<TARGET_FILE_DIR:StudentManager>/static
        COMMENT "Copying static folder..."
)

add_custom_target(copy_templates ALL
        COMMAND ${CMAKE_COMMAND} -E copy_directory
        ${CMAKE_SOURCE_DIR}/templates
        $<TARGET_FILE_DIR:StudentManager>/templates
        COMMENT "Copying templates folder..."
)

add_dependencies(StudentManager copy_static copy_templates)

